<?php

require_once '../../includes/az_mysqli.php';
require_once '../../classes/az_User.php';

$action = $_GET['act'];

//Function addUser
function add_new_user() {
    if (isset($_POST['username']) && isset($_POST['password'])) {
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
        $name = trim($_POST['name']);
        $phone = trim($_POST['phone']);
        $address = trim($_POST['address']);
        $sex = $_POST['sex'];
        $birthday = trim($_POST['birthday']);
        $email = trim($_POST['email']);
        $newUser = new Az_User();
        $newUser->createUser(null, $username, $password, $name, $birthday, $phone, $email, $address, 0, $sex, date('d/m/Y'), 0, 0);
        $result = $newUser->addNewUser();
        if ($result) {
            header('Location: index.php');
        }
    }
}

/*
 *  This function to update info or user
 */

function update_info_user() {
    if (isset($_POST['userId']) && is_numeric($_POST['userId'])) {
        $userId = $_POST['userId'];
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
        $name = trim($_POST['name']);
        $phone = trim($_POST['phone']);
        $address = trim($_POST['address']);
        $sex = $_POST['sex'];
        $birthday = trim($_POST['birthday']);
        $email = trim($_POST['email']);

        //Create new user object
        $user = new Az_User();
        $user->createUser($userId, $username, $password, $name, $birthday, $phone, $email, $address, 0, $sex, date('d/m/Y'), 0, 0);
        $result = $user->updateUser();
        if ($result) {
            header('Location: index.php');
        }
    }
}

/*
 *  Function change password of user
 */

function change_password() {
    
}

/*
 * function delete user
 */

function delete_user($userid) {
    if ($userid != null && is_numeric($userid)) {
        $user = new Az_User();
        $result = $user->deleteUser($userid);
        if ($result) {
            header('Location: index.php');
        }
    }
}

/*
 * Function active user
 */

function active_new_user() {
    
}

/*
 * Function change role for user
 */

function change_role_user() {
    
}

/*
 *  This function to delete all user if 
 *  User check all button
 */
function deleteAllUser(){
    if(isset($_POST['userId']) && is_array($_POST['userId'])){
        //header('Location: index.php');
        $user = new Az_User();
        $count = count($_POST['userId']);
        $tmpcount = 0;
        foreach ($_POST['userId'] as $userId){
            $result = $user->deleteUser($userId);
            if($result){
                $tmpcount +=1;
            }
        }
        if($count == $tmpcount){
            header('Location: index.php');
        }
    }
}

//Run action select
switch ($action) {
    case 1:
        add_new_user();
        break;
    case 2:
        update_info_user();
        break;
    case 3:
        if (isset($_GET['userId'])) {
            delete_user($_GET['userId']);
        }
        break;
    case 4:
        update_info_user();
        break;
    case 5:
        deleteAllUser();
        break;
    default:
        break;
}
?>
